package juc.mythread.create;

import com.sun.management.ThreadMXBean;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

class Tests1Thread {

    void testThreadMXBean() {
        ThreadMXBean threadMXBean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
        long[] ids = threadMXBean.getAllThreadIds();
        System.out.println("线程数量:" + ids.length);
        //ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
        ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
        /*
        * 线程名称:main
        线程名称:Reference Handler
        线程名称:Finalizer
        线程名称:Signal Dispatcher
        线程名称:Attach Listener
        线程名称:Common-Cleaner
        线程名称:Monitor Ctrl-Break
        线程名称:Notification Thread
        线程名称:RMI TCP Accept-0
        * */
        for (ThreadInfo threadInfo : threadInfos) {
            //属性的作用
            //System.out.println("线程ID:" + threadInfo.getThreadId());
            System.out.println("线程名称:" + threadInfo.getThreadName());
            //System.out.println("线程状态:" + threadInfo.getThreadState());
            //System.out.println("线程CPU使用时间:" + threadMXBean.getThreadCpuTime(threadInfo.getThreadId()));
            //System.out.println("线程用户时间:" + threadMXBean.getThreadUserTime(threadInfo.getThreadId()));
            //System.out.println("线程堆栈跟踪:" + threadInfo.getStackTrace());
            //System.out.println("线程锁信息:" + threadInfo.getLockInfo());
            //System.out.println("线程锁持有者:" + threadInfo.getLockOwnerName());
            //System.out.println("线程锁等待时间:" + threadInfo.getBlockedTime());
            //System.out.println("线程等待时间:" + threadInfo.getWaitedTime());
            //System.out.println("线程是否被挂起:" + threadInfo.isSuspended());
            //System.out.println("线程是否被守护线程:" + threadInfo.isDaemon());
            //System.out.println("线程优先级:" + threadInfo.getPriority());

        }
    }

    void testThread() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程开始执行:myThread1" + Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程执行完毕---myThread1");
            }
        }, "myThread1");
        thread.start(); //自己的线程
        //thread.run();//主线程

        Thread thread2 = new Thread(() -> {
            System.out.println("线程开始执行:thread2" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程执行完毕---thread2");
        }, "thread2");
        thread2.start(); //自己的线程
    }

    public void testFutureTask() {
        //FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
        //    @Override
        //    public Integer call() throws Exception {
        //        System.out.println("线程开始执行:" + Thread.currentThread().getName());
        //        Thread.sleep(1000);
        //        System.out.println("线程执行完毕---");
        //        return 1;
        //    }
        //});

        FutureTask<Integer> futureTask = new FutureTask<>(() -> {
            System.out.println("线程开始执行:myThread3 " + Thread.currentThread().getName());
            Thread.sleep(2000);
            System.out.println("线程执行完毕 myThread3");
            return 1;
        });
        Thread thread3 = new Thread(futureTask, "myThread3");
        thread3.start(); //自己的线程
        try {
            Integer result = futureTask.get(10, TimeUnit.SECONDS);
            System.out.println("result:" + result);
            System.out.println("result ---222");
        } catch (InterruptedException e) {
            System.err.println("线程被中断: " + e.getMessage());
            Thread.currentThread().interrupt(); // 恢复中断状态
        } catch (ExecutionException e) {
            System.err.println("任务执行异常: " + e.getMessage());
        } catch (TimeoutException e) {
            System.err.println("任务执行超时: " + e.getMessage());
        }
    }
}
